# author:    Yannick Stiller, A. Duer, Robert A. Huber
# contact:   robert.huber@sbg.ac.at
# file name: descriptives.R
# Context:   Provides descriptiv statistics printed in Stiller et al World Trade Review

#Empty memory
rm(list = ls())

#Load data
df <- read_rds("./created_data/df_analysis.rds")

#Loads necessary packages
library(tidyverse)

#define useful functions for later
'%!in%' <- function(x,y)!('%in%'(x,y))

#### Descriptive information in text ####

sec4.2_edu <- as.data.frame(prop.table(table(df$country_short, df$edu2),1))
sec5.3_trade <- as.data.frame(prop.table(table(df$trade_support)))

#### Descriptive charts ####
# this section provides descriptives displayed in section A3 of the online appendix

# .Trade Support
df %>%
  filter(!is.na(trade_support)) %>%
  ggplot(., aes(x=trade_support))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Opinion towards growing trade and business ties") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/trade_support.pdf",
       width = 8, height = 6, units = "cm")

# .Jobs
df %>%
  filter(!is.na(consequences_jobs)) %>%
  ggplot(., aes(x=consequences_jobs))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Beliefs about effect of trade on jobs") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/consequences_jobs.pdf",
       width = 8, height = 6, units = "cm")

# .Wages
df %>%
  filter(!is.na(consequences_wages)) %>%
  ggplot(., aes(x=consequences_wages))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Beliefs about effect of trade on wages") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/consequences_wages.pdf",
       width = 8, height = 6, units = "cm")

# .Education
df %>%
  filter(!is.na(edu2)) %>%
  ggplot(., aes(x=edu2))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Education") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/education.pdf",
       width = 8, height = 6, units = "cm")

# Employment
df %>%
  filter(!is.na(employment)) %>%
  ggplot(., aes(x=employment))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Employment") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/employment.pdf",
       width = 8, height = 6, units = "cm")

# .Age
df %>%
  filter(!is.na(age)) %>%
  ggplot(., aes(x=age))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Age groups") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/age.pdf",
       width = 8, height = 6, units = "cm")

# .Gender
df %>%
  filter(!is.na(female)) %>%
  ggplot(., aes(x=female))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Gender") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/female.pdf",
       width = 8, height = 6, units = "cm")

# .Economic left-right
df %>%
  filter(!is.na(economic_left_right)) %>%
  ggplot(., aes(x=economic_left_right))+
  geom_bar(colour = "black", fill = "white") +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Economic left-right placement") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/economic_left_right.pdf",
       width = 8, height = 6, units = "cm")

# .LGNIC
df %>%
  filter(!is.na(lgnic)) %>%
  ggplot(., aes(x=lgnic))+
  geom_histogram(colour = "black", fill = "white", bins = 50) +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Logged regional GNI per capita") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/lgnic.pdf",
       width = 8, height = 6, units = "cm")

# .Subnational Trade Competitiveness
df %>%
  filter(!is.na(competitiveness)) %>%
  ggplot(., aes(x=competitiveness))+
  geom_histogram(colour = "black", fill = "white", bins = 50) +
  ggthemes::theme_tufte() +
  labs(y="Count", x = "Subnational trade competitiveness") +
  theme(text = element_text(size = 9)) +
  NULL

ggsave(filename = "./figures/descriptives/rca.pdf",
       width = 8, height = 6, units = "cm")

# Descriptives table ####
desc_table <- df %>%
  mutate(edu2 = as.numeric(edu2)-1,
         female = as.numeric(female)-1,
         employment = as.numeric(employment)-1,
         age_young = ifelse(age == "young", 1, 0),
         age_middle = ifelse(age == "middle", 1, 0),
         age_old = ifelse(age == "old", 1, 0),
         trade_support = as.numeric(trade_support),
         consequences_jobs = as.numeric(consequences_jobs),
         consequences_wages = as.numeric(consequences_wages)) %>%
  psych::describe(.) %>%
  mutate(variable = row.names(.)) %>%
  filter(variable %in% c("consequences_jobs", "consequences_wages", "trade_support", "edu2", "employment", "age_young", "age_middle", "age_old", "female", "economic_left_right", "lgnic", "competitiveness")) %>%
  mutate(Variable = car::recode(variable, "'edu2' = 'Education (tertiary)'; 'competitiveness' = 'Subnational trade competitiveness'; 'age_young' = 'Age (young)'; 'age_middle' = 'Age (middle)'; 'age_old' = 'Age (old)';
                                'female' = 'Gender (female)'; 'employment' = 'Employment (employed)'; 'lgnic' = 'Logged Regional GNI per capita'; 'economic_left_right' = 'Economic left-right placement (4-point scale)';
                                'trade_support' = 'Support for trade (4-point scale)'; 'consequences_jobs' = 'Consequences for jobs (3-point scale)'; 'consequences_wages' = 'Consequences for wages (3-point scale)'"),
         order = car::recode(variable, "'consequences_jobs' = 1; 'consequences_wages' = 2; 'trade_support' = 3;
                             'edu2' = 4; 'employment' = 5; 'age_young' = 6; 'age_middle' = 7; 'age_old' = 8;
                             'female' = 9; 'economic_left_right' = 10; 'lgnic' = 11; 'competitiveness' = 12")) %>%
  dplyr::arrange(., order) %>%
  dplyr::select(Variable = Variable, 
                N = n,
                Mean = mean,
                SD = sd,
                Min = min,
                Max = max) %>%
  mutate(Missing = 1-N/nrow(df)) %>%
  rename("Share missing" = "Missing")

desc_out <- xtable::xtable(desc_table, 
                           label = "tab:descriptives",
                           digits = c(0, 0, 0, 2, 2, 2, 2, 2), 
                           align = c("l", "l", "r", "r", "r", "r", "r", "r"),
                           caption = "Descriptive statistics of variables")

print(desc_out, 
      include.rownames = F,
      type="latex", file="./tables/tableA1.tex", label = "tabA1") 

#### Maps ####
#Provides the map displayed in section A4.

countries_pew <- c("ARG", "BGD", "BRA", "CHL", "CHN", "COL", "DEU", "EGY", "ESP", "FRA",
                   "GBR", "GHA", "GRC", "IDN", "IND", "ISR", "ITA", "JOR", "JPN", "KEN", 
                   "KOR", "LBN", "MEX", "MYS", "NGA", "NIC", "PAK", "PER", "PHL", "POL",
                   "PSE", "RUS", "SEN", "SLV", "THA", "TUN", "TUR", "TZA", "UGA", "UKR",
                   "USA", "VEN", "VNM", "ZAF")

df_map <- data.frame(country_code = countries_pew) %>%
  mutate(Sample = ifelse(country_code %in% df$country_short, "Included in analysis", "Excluded from analysis")) %>%
  mutate(country = countrycode::countrycode(country_code, origin = "iso3c", destination = "country.name"),
         region = car::recode(country,
                              "'United Kingdom' = 'UK'; 'United States' = 'USA'")) %>%
  left_join(map_data("world"), ., by = "region") %>%
  mutate(Sample = ifelse(is.na(Sample), "Not in PEW Survey", Sample),
         Sample = factor(Sample, levels = c("Included in analysis", "Excluded from analysis", "Not in PEW Survey")))

ggplot(df_map, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = Sample), colour = "white", size = .01) + 
  scale_fill_manual(values = c("darkblue", "lightblue", "lightgray"), na.value = "lightgray") +
  ggthemes::theme_tufte() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) + 
  ggtitle("Country overview") +
  theme(legend.position = "bottom") +
  NULL

ggsave(filename = "./figures/descriptives/map.pdf",
       height = 10,
       width = 20, units = "cm")



